package com.project.web.admin.controller.core;

import com.project.common.web.utils.file.FileUtil;
import com.project.model.core.StdCode;
import com.project.model.core.StdCodeType;
import com.project.web.admin.controller.common.BaseController;
import com.project.web.admin.service.core.StdCodeService;
import com.project.web.admin.service.core.StdCodeTypeService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * 标准编码controller
 */
@Controller("adminStdCodeController")
@RequestMapping("/admin/stdCode")
public class StdCodeController extends BaseController {

    private static final long serialVersionUID = -876914729302594823L;

    @Resource(name = "stdCodeTypeServiceImpl")
    private StdCodeTypeService stdCodeTypeService;

    @Resource(name = "stdCodeServiceImpl")
    private StdCodeService stdCodeService;

    /**
     * 列表
     *
     * @return
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public String list(Long typeId, Model model) {

        StdCodeType stdCodeType = stdCodeTypeService.getById(typeId);
        model.addAttribute("list", stdCodeService.findList(stdCodeType, true));
        model.addAttribute("codeType", stdCodeType);

        return "/intfc/core/stdCode/list.ftl";
    }

    /**
     * 添加
     *
     * @return
     */
    @RequestMapping(value = "/add", method = RequestMethod.GET)
    public String add(Long typeId, Model model) {
        StdCodeType stdCodeType = stdCodeTypeService.getById(typeId);
        if (stdCodeType == null) {
            return ERROR_VIEW;
        }
        model.addAttribute("codeType", stdCodeType);
        return "/intfc/core/stdCode/add.ftl";
    }

    /**
     * 保存
     *
     * @return
     */
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public String save(StdCode stdCode, Long typeId, HttpServletRequest request, RedirectAttributes redirectAttributes) {
        StdCodeType stdCodeType = stdCodeTypeService.getById(typeId);
        if (stdCodeType == null) {
            return ERROR_VIEW;
        }

        if (stdCode.getIsEnabled() == null) {
            stdCode.setIsEnabled(false);
        }
        stdCode.setCodeType(stdCodeType.getId());
        stdCode.setCodeIco(FileUtil.copyForPlUpload(request, stdCode.getCodeIco(), FileUtil.CODE_ICO));
        try {
            stdCodeService.save(stdCode);
        } catch (Exception e) {
            e.printStackTrace();
        }
        redirectAttributes.addAttribute("typeId", stdCodeType.getId());
        addFlashMessage(redirectAttributes, SUCCESS_MESSAGE);
        return "redirect:list.jhtml";
    }

    /**
     * 编辑
     *
     * @return
     */
    @RequestMapping(value = "/edit", method = RequestMethod.GET)
    public String edit(Long id, Model model) {
        StdCode stdCode = stdCodeService.getById(id);
        if (stdCode == null) {
            return ERROR_VIEW;
        }
        model.addAttribute("stdCode", stdCode);
        return "/intfc/core/stdCode/edit.ftl";
    }

    /**
     * 更新
     *
     * @return
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public String update(StdCode stdCode, Long typeId, HttpServletRequest request, RedirectAttributes redirectAttributes) {
        StdCodeType stdCodeType = stdCodeTypeService.getById(typeId);
        if (stdCodeType == null) {
            return ERROR_VIEW;
        }

        if (stdCode.getIsEnabled() == null) {
            stdCode.setIsEnabled(false);
        }
        stdCode.setCodeType(stdCodeType.getId());
        FileUtil.updateForUpload(request, stdCode, stdCode, FileUtil.CODE_ICO, new String[]{"codeIco"});
        stdCodeService.update(stdCode);
        redirectAttributes.addAttribute("typeId", stdCodeType.getId());
        addFlashMessage(redirectAttributes, SUCCESS_MESSAGE);
        return "redirect:list.jhtml";
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/delete", method = RequestMethod.GET)
    public String delete(Long id, RedirectAttributes redirectAttributes) {

        StdCode stdCode = stdCodeService.getById(id);

        if (stdCode == null) {
            return ERROR_VIEW;
        }

        stdCodeService.delete(stdCode.getId());
        redirectAttributes.addAttribute("typeId", stdCode.getCodeType());
        addFlashMessage(redirectAttributes, SUCCESS_MESSAGE);

        return "redirect:list.jhtml";
    }
}
